<?php

namespace adminmatrix\server\app\logic;
use adminmatrix\server\app\server\JsonService;
use think\exception\HttpException;
use think\Request;
use think\App;

abstract class BaseLogic
{
    use LogicParamsTrait;

    protected App $app;
    protected Request $request;
    public function __construct(App $app)
    {
        $this->app      = $app;
        $this->request  = Request();
        $this->getParams();
        $this->app->bind('jsonService' ,JsonService::class);

    }

    public function __call($method, $args)
    {
        halt($method);
    }


    public function save(  Request $request ) : bool
    {
        if ($this->model->save($request->post())) return  true;
        return throw new HttpException(200 , '新增失败');
    }


    public function update( Request $request) : bool
    {
        $params = $request->put();
        if (empty($params['id'])) return throw new HttpException(200 , '缺少必要查询参数id');
        $data = $this->model->findOrEmpty($params['id']);
        if ($data->save($params)) return true;
        return throw new HttpException(200 , '更新失败');
    }


    public function delete(int $id) : bool
    {
        if (empty($id)) return throw new HttpException(200 , '缺少必要查询条件id');
        $data = $this->model->findOrEmpty($id);
        if ($data->delete()) return true;
        return throw new HttpException(200 , '删除失败');
    }
}